package com.bball.court.ui;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;

import com.bball.court.PreferenceHelper;
import com.bball.court.R;
import com.bball.court.ui.fragment.CourtsListFragment;
import com.bball.court.ui.fragment.FormFragment;
import com.bball.court.ui.fragment.ProfileFragment;

public class NavigationUtil {

    public enum NextScreen {
        Profile, Suggestion
    }

    public static final int requestLoginCode = 654;

    public static void goToHome(Context context) {
        context.startActivity(MainActivity.newIntent(context));
    }

    public static void goToDetail(Context context, String eventId) {
        context.startActivity(DetailActivity.newIntent(context, eventId));
    }

    public static void goToFormFragment(Activity activity, FragmentManager fragmentManager) {
        if (PreferenceHelper.isLoggedIn(activity)) {
            goToFragment(fragmentManager, R.id.suggestion_frame, FormFragment.newFragment());
        } else {
            activity.startActivityForResult(LoginActivity.newIntent(activity), requestLoginCode);
        }
    }

    public static void goToProfile(Activity activity, FragmentManager fragmentManager) {
        if (PreferenceHelper.isLoggedIn(activity)) {
            goToFragment(fragmentManager, R.id.content_frame, ProfileFragment.newFragment());
        } else {
            activity.startActivityForResult(LoginActivity.newIntent(activity), requestLoginCode);
        }
    }

    private static void goToFragment(FragmentManager fragmentManager, int frameId, Fragment fragment) {
        fragmentManager.beginTransaction().replace(frameId, fragment).commit();
    }

    public static void handleMenuClick(Activity activity, int position) {
        FragmentManager fragmentManager = activity.getFragmentManager();
        switch (position) {
        case 0:
            goToHome(activity);
            break;
        case 1:
            goToFragment(fragmentManager, R.id.content_frame, CourtsListFragment.newFragment());
            break;
        case 2 :
            activity.startActivity(SuggestionActivity.newIntent(activity));
            break;
        case 3 :
            goToProfile(activity, fragmentManager);
            break;
        default:
            throw new IllegalStateException("Not yet implementted");
        }
    }

}
